import * as THREE from "three";
// 引入动画库
import gsap from "gsap";
export default function modifyCityMaterial(mesh){
    // console.log(mesh)
   
    // 修改着色器渲染
    // .onBeforeCompile ( shader : Shader, renderer : WebGLRenderer ) : undefined
    // 在编译shader程序之前立即执行的可选回调。此函数使用shader源码作为参数。用于修改内置材质。
    // 和其他属性不一样的是，这个回调在.clone()，.copy() 和 .toJSON() 中不支持。
    mesh.material.onBeforeCompile = (shader)=>{
        // console.log(shader.vertexShader);
        // console.log(shader.fragmentShader);
        shader.fragmentShader =  shader.fragmentShader.replace(
            `#include <dithering_fragment>`,
            `#include <dithering_fragment>
            //#end#
            `
        );
        addGradColor(shader,mesh)
        addSpread(shader);
        addLightLine(shader);
        addToTopLine(shader)
    }
}

export function addGradColor(shader,mesh){
    mesh.geometry.computeBoundingBox();
    // 当前 bufferGeometry 的外边界矩形。可以通过 .computeBoundingBox() 计算。默认值是 null。
    // console.log(mesh.geometry.boundingBox);
    // 获取物体的高度差
    let {min,max} = mesh.geometry.boundingBox;
    let uHeight = max.y - min.y;

    shader.uniforms.uTopColor = {
        value:new THREE.Color("#aaaeff")
    }
    shader.uniforms.uHeight = {
        value:uHeight
    }
    // vertexShader
    shader.vertexShader = shader.vertexShader.replace(
        `#include <common>`,
        `#include <common>
        varying vec3 vPosition;
        `
    );
    shader.vertexShader = shader.vertexShader.replace(
        `#include <begin_vertex>`,
        `#include <begin_vertex>
        vPosition = position;
        `
    );
    // fragmentShader
    shader.fragmentShader =  shader.fragmentShader.replace(
        `#include <common>`,
        `#include <common>
        varying vec3 vPosition;
        uniform vec3 uTopColor;
        uniform float uHeight;
        `
    );
    shader.fragmentShader =  shader.fragmentShader.replace(
        `//#end#`,
        `vec4 distGradColor = gl_FragColor;

        // 设置混合的百分比
        float gradMix = (vPosition.y+uHeight/4.0)/uHeight;
        // 计算出混合颜色
        vec3 gradMixColor = mix(distGradColor.xyz,uTopColor,gradMix);
        gl_FragColor = vec4(gradMixColor,1);
        //#end#
        `
    );

}

export function addSpread(shader){
    // 设置扩散的中心点
    shader.uniforms.uSpreadCenter = {value:new THREE.Vector2(0,0)};
    // 扩散的时间
    shader.uniforms.uSpreadTime ={value:0}
    // 设置条带的宽度
    shader.uniforms.uSpreadWidth ={value:40};
    shader.fragmentShader =  shader.fragmentShader.replace(
        `#include <common>`,
        `#include <common>
        uniform vec2 uSpreadCenter;
        uniform float uSpreadTime;
        uniform float uSpreadWidth;
        `
    );
    // 扩散效果
    shader.fragmentShader =  shader.fragmentShader.replace(
        `//#end#`,
        `
        float spreadRadius = distance(vPosition.xz,uSpreadCenter);

        float spreadIndex = -(spreadRadius-uSpreadTime)*(spreadRadius-uSpreadTime)+uSpreadWidth;

        if(spreadIndex>0.0){
            gl_FragColor = mix(gl_FragColor,vec4(1,1,1,1),spreadIndex/uSpreadWidth);
        }

        //#end#
        `
    );
    
    gsap.to(shader.uniforms.uSpreadTime,{
        value:600,
        duration:2,
        ease:"none",
        repeat:-1
    });
}

export function addLightLine(shader){
    // 扩散的时间
    shader.uniforms.uLightLineTime ={value:-1200}
    // 设置条带的宽度
    shader.uniforms.uLightLineWidth ={value:40};
    shader.fragmentShader =  shader.fragmentShader.replace(
        `#include <common>`,
        `#include <common>
        uniform float uLightLineTime;
        uniform float uLightLineWidth;
        `
    );
    // 光带掠过效果
    shader.fragmentShader =  shader.fragmentShader.replace(
        `//#end#`,
        `
        // float LightLineMix = -(vPosition.x-uLightLineTime)*(vPosition.x-uLightLineTime)+uLightLineWidth;
        float LightLineMix = -(vPosition.x+vPosition.z-uLightLineTime)*(vPosition.x+vPosition.z-uLightLineTime)
        +uLightLineWidth;

        if(LightLineMix>0.0){
            gl_FragColor = mix(gl_FragColor,vec4(1,0.5,0.5,1),LightLineMix/uLightLineWidth);
        }

        //#end#
        `
    );
    
    gsap.to(shader.uniforms.uLightLineTime,{
        value:600,
        duration:2,
        ease:"none",
        repeat:-1
    });
}

export function addToTopLine(shader){
    // 扩散的时间
    shader.uniforms.uToTopLineTime ={value:0}
    // 设置条带的宽度
    shader.uniforms.uToTopLineWidth ={value:20};
    shader.fragmentShader =  shader.fragmentShader.replace(
        `#include <common>`,
        `#include <common>
        uniform float uToTopLineTime;
        uniform float uToTopLineWidth;
        `
    );
    // 从下到上扫描效果
    shader.fragmentShader =  shader.fragmentShader.replace(
        `//#end#`,
        `
        float ToTopLineMix = -(vPosition.y-uToTopLineTime)*(vPosition.y-uToTopLineTime)+uToTopLineWidth;


        if(ToTopLineMix>0.0){
            gl_FragColor = mix(gl_FragColor,vec4(0.5,0.5,1,1),ToTopLineMix/uToTopLineWidth);
        }

        //#end#
        `
    );
    
    gsap.to(shader.uniforms.uToTopLineTime,{
        value:1000,
        duration:5,
        ease:"none",
        repeat:-1
    });
}